package cn.alcatraz.org.service.impl;

import cn.alcatraz.basic.service.impl.BaseServiceImpl;
import cn.alcatraz.basic.utils.PageList;
import cn.alcatraz.org.domain.Department;
import cn.alcatraz.org.mapper.DepartmentMapper;
import cn.alcatraz.org.query.DepartmentQuery;
import cn.alcatraz.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
// 只读事务

public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

   

    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        //dirPath拼接值
        String dirPath = "";
        if (department.getParent().getId() == null){
            dirPath = "/" + department.getId();
        }else {
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }
    /**
    * @Title: update
    * @Description: 拼接地址级
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/6/26 10:22
    * @Parameters: [department]
    * @Return void
    */
    @Override
    @Transactional
    public void update(Department department) {
        //dirPath拼接值
        String dirPath = "";
        if (department.getParent().getId() == null){
            dirPath = "/" + department.getId();
        }else {
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

   
   


    @Override
    public List<Department> tree() {

        List<Department> depts = departmentMapper.loadAll();

        List<Department> deptTree = new ArrayList<Department>();

        HashMap<Long, Department> deptMap = new HashMap<>();
        for (Department department : depts) {
            deptMap.put(department.getId(), department);
        }

        //从所有数据中找到一级数据
        for (Department deptTmp : depts) {
            //数据没有父级Id，就是一级数据
            if(deptTmp.getParentId() == null){
                deptTree.add(deptTmp);
            }else {
                Department department = deptMap.get(deptTmp.getParentId());
                if (department != null){
                    department.getChildren().add(deptTmp);
                }
            }
        }

        return deptTree;
    }

}
